//
//  DrawView.m
//  绘制图形
//
//  Created by student on 16/4/6.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self drawGraphics1];
    [self drawGraphics2];
    [self drawGraphics3];
    [self drawGraphics4];
    [self drawGraphics5];
    [self drawGraphics6];
//    [self drawGraphics7];
    [self drawGraphics8];
}

// 方法1 ： 在图形上下添加路径

- (void)drawGraphics1
{
    //1 得到图形上下
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //2 添加路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    //设置起点
    CGPathMoveToPoint(path, NULL, 50, 50);
    
    //设置终点
    CGPathAddLineToPoint(path, NULL, 200, 200);
    
    //3 把路径添加到图形上下文中
    CGContextAddPath(ctx, path);
    
    //4 绘制出来
    CGContextStrokePath(ctx);


}

// 方法2
- (void)drawGraphics2
{
    // 1得到图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 设置起点
    CGContextMoveToPoint(ctx, 50, 50);
    
    //设置终点
    CGContextAddLineToPoint(ctx, 150, 200);
    
    // 绘制出来
    CGContextStrokePath(ctx);
}

// 方式3
- (void)drawGraphics3
{
    //贝塞尔 路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(250, 200)];
    //绘制路径
    [path stroke];
}



- (void)drawGraphics4
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    
    //设置当前stroke颜色
    [[UIColor redColor] setStroke];
    
    // 线宽
    CGContextSetLineWidth(ctx, 10);
    // 线的接头的地方的样式
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);
    //    kCGLineJoinMiter 直角
    //    kCGLineJoinRound 圆角
    //    kCGLineJoinBevel 缺角
    
    CGContextStrokePath(ctx);
}


- (void)drawGraphics5
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(50, 100)];
    path.lineWidth = 10;
    [[UIColor greenColor] setStroke];
    [path stroke];
    
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(50, 100)];
    [path2 addLineToPoint:CGPointMake(50, 150)];
    path2.lineWidth = 10;
    [[UIColor yellowColor] setStroke];
    [path2 stroke];
}

- (void)drawGraphics6
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 50, 200);
    CGContextAddQuadCurveToPoint(ctx, 150, 250, 250, 200);// 控制点的xy轴 终点的xy轴
    [[UIColor greenColor] setStroke];
    [[UIColor blackColor] setFill];
    
    CGContextFillPath(ctx);
}


- (void)drawGraphics7
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(30, 30, 200, 200) cornerRadius:100];//(cornerRadius:100)设置四个棱角的弧度
    [[UIColor redColor] setStroke];//线的颜色
    [[UIColor greenColor] setFill];// 线的填充颜色
    path.lineWidth = 30;// 线宽
    [path stroke];
    [path fill];
}

- (void)drawGraphics8
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(125, 125)];
    
    // 中心点 半径 起始角度 终止角度 顺逆方向
    [path addArcWithCenter:CGPointMake(125, 125) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    [path closePath];
    [[UIColor whiteColor] setStroke];// 线为白色  在不规定填充颜色的时候 自动填充黑色
//    [[UIColor purpleColor] setFill];//填充颜色为紫色
    
    path.lineWidth = 10;// 线宽
    
    [path stroke];
    [path fill];
}

@end
